/**
 * @author Mahesh
 */

window.addEventListener('load', settingsOnloadFunction, false);
function settingsOnloadFunction()
{
	if(document.title != "Settings") return; 
	
	// Navigation link event listeners
	document.getElementById("microBreakSettings").onclick = displaySettingsPage;
	document.getElementById("macroBreakSettings").onclick = displaySettingsPage;
	document.getElementById("otherOptionsSettings").onclick = displaySettingsPage;
	
	// Range selectors display events listeners
	document.getElementById("microBreakDurationRange").addEventListener('change', setTimerSliderEventListeners, false); 
	document.getElementById("microBreakFrequencyRange").addEventListener('change', setTimerSliderEventListeners, false); 
	
	// User preference local storage event listeners
	document.getElementById("microBreakDurationRange").addEventListener('change', persistMicroIntervalDurationRange, false);
	document.getElementById("microBreakFrequencyRange").addEventListener('change', persistMicroIntervalFrequencyRange, false);
} 

function persistMicroIntervalDurationRange(){
	localStorage.setItem('micro_break_duration_minutes', this.value/60);
}

function persistMicroIntervalFrequencyRange() {
	localStorage.setItem('micro_break_frequency_minutes', this.value);
}

//Add event handlers to range type inputs so user can see the current value as he 
//uses the slider
function setTimerSliderEventListeners() {
	document.getElementById(this.getAttribute("spanID")).innerHTML  = this.value;
}

// Display the appropriate page based on user selection in the navigation pane
function displaySettingsPage()
{
	// Change the style of the <li> the user clicked on
	setNavigationSelectedItem(this.id);
	
	var pageItems = document.getElementById("settings-pane").getElementsByClassName('page');
	for(var i in pageItems)
	{
		// Some elements with undefined ids are creeping in. Did not figure out what they are and 
		// how to avoid them, so this temporary workaround to skip those items. 
		if(!pageItems[i].id) continue;
		
		if (pageItems[i].id == this.getAttribute("pageID")) {
			pageItems[i].classList.remove('hidden');
		} else {
			pageItems[i].classList.add('hidden');
		}
	}
}

// Change the style of the selected navigation link to indicate that it is selected
function setNavigationSelectedItem(elementID) {
	var navigationItems = document.getElementById("navigation-pane").getElementsByTagName('li');
	for(var i in navigationItems) {
		
		if(!navigationItems[i].id) continue;
	
		if (navigationItems[i].id == elementID) {
			navigationItems[i].classList.add('navigation-pane-item-selected');
		} else {
			navigationItems[i].classList.remove('navigation-pane-item-selected');
		}
	}
}

// Load the default values and store them in localStorage. Check for null so we don't
// overwrite any user defined preferences
function loadDefaults()
{
	if(localStorage.getItem('micro_break_frequency_minutes') == null) {
		localStorage.setItem('micro_break_frequency_minutes', MICRO_BREAK_FREQUENCY_MINUTES);
	}
	
	if(localStorage.getItem('micro_break_duration_minutes') == null) {
		localStorage.setItem('micro_break_duration_minutes', MICRO_BREAK_DURATION_MINUTES);
	}
	
	if(localStorage.getItem('playSound') == null) {
		localStorage.setItem('playSound', true);
	}
}